﻿using System;
using System.Runtime.InteropServices;

namespace mb
{
    public class Logger : mb.ILog
    {
#if __IOS__
        [DllImport(ObjCRuntime.Constants.FoundationLibrary)]
        private extern static void NSLog(IntPtr message);
#endif

        public void Log(params object[] logs)
        {
            if (mb.Util.HasTestFlag == 0) return;

            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            for (int i = 0; i < logs.Length; ++i)
            {
                sb.Append(logs[i] != null ? logs[i].ToString() : "null");
                if (i < logs.Length - 1)
                    sb.Append(", ");
            }
            //var szMsg = string.Format("[{0}]{1}", DateTime.Now.ToString("HH:mm:ss.ffff"), sb.ToString());

            string szMsg = sb.ToString();
#if __IOS__
            string prefix = "STDOUT";
            int limit = 1000;
            for (int i = 0; i < szMsg.Length; i += limit)
            {
                int writeLen = Math.Min(szMsg.Length - i, limit);
                string pp = szMsg.Length < limit?$"{prefix} ": $"{prefix}.{i}: ";
                Console.WriteLine(pp + szMsg.Substring(i, writeLen));
            }
#else
            Console.WriteLine(szMsg);
#endif
        }

    }
}